using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Typing_GUI.CLASS;
//using Typing_GUI.CLASS
namespace Typing_GUI
{
    public partial class frmInformation : Form
    {
        public int userID;
        CLASS.CNguoiDung user;
        string oldUrl = "";
        public frmInformation()
        {
            InitializeComponent();
        }
        void SetInformation()
        {
            string un=CLASS.CNguoiDung.SetAvatar_Level(userID, pbAvatar, pbLevel);
         
            
            
            user = CLASS.CNguoiDung.GetUserById(userID);
            lblUsername.Text = user.Tendangnhap;
            if (user == null)
            {
                this.Close();
            }
            else
            {
                lblEmail.Text = user.Email;
//                lblFullname.Text = user.Hoten;
                txtFullName.Text = user.Hoten;
                if (user.Gioitinh == true)
                {
//                    lblGender.Text = "Male";
                    rbMale.Checked = true;
                }
                else
                {
//                    lblGender.Text = "Female";
                    rbFemale.Checked = true;
                }
//                lblBirthday.Text = user.Ngaysinh;
                dtpBirthday.Value = DateTime.Parse(user.Ngaysinh.ToString());
//                lblAddress.Text = user.Diachi;
                txtAddress.Text = user.Diachi;
//                lblScore.Text = user.Tongdiem.ToString();
                oldUrl = user.Urlhinhanh;

                lblScore.Text = user.Tongdiem.ToString();
            }
        }
        private void frmInformation_Load(object sender, EventArgs e)
        {
            SetInformation();
        }

        private void btnBackToMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Images |*.jpg;*.jpeg;*.gif;*.png;*.bmp";
            dlg.Multiselect = false;
            dlg.RestoreDirectory = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtBrowse.Text = dlg.FileName;
                btnChangeAvatar.Enabled = true;
            }
        }

        private void btnChangeAvatar_Click(object sender, EventArgs e)
        {

            
            if (txtBrowse.Text != "")
            {
                btnChangeAvatar.Enabled = false;
               FileInfo file = new FileInfo(txtBrowse.Text);
               if (file.Length > 1000000)
               {
                   MessageBox.Show("File size must less than 1MB", "Message");
               }
               else
               {
                   if (Directory.Exists("images/Avatar/"))
                   {

                       string fn = System.Guid.NewGuid().ToString() + file.Name;
                       File.Copy(file.FullName, "images/Avatar/" + fn);

                       CLASS.CNguoiDung.UpdateAvatar(userID, fn);

                       CLASS.CNguoiDung.SetAvatar_Level(userID, pbAvatar, pbLevel);

                      
                   }
                   else
                   {
                       MessageBox.Show("Directory does not exist", "Message");
                   }
               }
            }
        }

        private void frmInformation_FormClosed(object sender, FormClosedEventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtOldPassword.Text = "";
            txtNewPassword.Text = "";
            txtConfirm.Text = "";
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            int type=0;
            bool flag = CLASS.CNguoiDung.KiemTraNguoiDung(user.Tendangnhap, txtOldPassword.Text, ref type);
            if (!flag)
            {
                MessageBox.Show("Old password do not match!", "Message");
                txtOldPassword.Text = "";
                txtOldPassword.Focus();
            }
            else
            {
                if (txtNewPassword.Text.Length < 6)
                {
                    MessageBox.Show("Password length must great than 5 charaters", "Messag");
                    txtNewPassword.Text = "";
                    txtNewPassword.Focus();

                }
                else
                {
                    if (txtConfirm.Text != txtNewPassword.Text)
                    {
                        MessageBox.Show("Confirm password do not match!", "Message");
                        txtConfirm.Text = "";
                        txtConfirm.Focus();
                    }
                    else
                    {
                        user.Matkhau = txtNewPassword.Text;
                        if (CLASS.CNguoiDung.UpdatePassword(userID, txtNewPassword.Text))
                            MessageBox.Show("Password have changed successful!", "Message");
                        else
                            MessageBox.Show("Have have changed failed!", "Message");
                    }
                }
            }
        }

        private void txtBrowse_Click(object sender, EventArgs e)
        {
            btnBrowse_Click(sender, e);
        }

        public bool KiemTraNgaySinh(DateTime dt)
        {
            if (DateTime.Today.Year - dt.Year > 100 || DateTime.Today.Year - dt.Year < 10)
                return false;
            return true;
        }

        public bool KiemTraCapNhat()
        {

            if (txtFullName.Text == "")
            {
                MessageBox.Show("Fill Full Name  !! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtFullName.Focus();
                return false;

            }
            else if (KiemTraNgaySinh(dtpBirthday.Value) == false)
            {
                MessageBox.Show("Your BirthDay is incorrect \n Min = 10, Max= 100  !! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dtpBirthday.Focus();
                return false;

            }
            else if (txtAddress.Text == "" || txtAddress.Text.Length < 10)
            {
                MessageBox.Show("Address you should fill it  ! At least 10 characters ! ", "Warning ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtAddress.Focus();
                return false;
            }
            return true;
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (KiemTraCapNhat() == true)
            {
                CNguoiDung nguoidung = new CNguoiDung();
                nguoidung.Hoten = txtFullName.Text;
                nguoidung.Diachi = txtAddress.Text;
                nguoidung.Ngaysinh = dtpBirthday.Value.ToString("dd/MM/yyyy");
                if (rbMale.Checked == true)
                    nguoidung.Gioitinh = true;
                else
                    nguoidung.Gioitinh = false;
                nguoidung.Manguoidung = userID;
                nguoidung.CapNhatNguoiDung();
                MessageBox.Show("Update complete !!!","Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}